package com.front.web.shiro.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.front.web.controller.BaseController;


/**
 * 
 * LoginController.java
 * <p>Copyright: Copyright (c) 2015 <p> 
 * <p>Company: xinghuo</p>
 *  @author    SeanXiao
 *  @version   1.0
 */
@Controller
public class LoginController extends BaseController
{

    @RequestMapping(value = "/login")
    public String showLoginForm(HttpServletRequest req,ModelMap map)throws Exception
    {
        String exceptionClassName = (String) req.getAttribute("shiroLoginFailure");
        String error = "";
        if (UnknownAccountException.class.getName().equals(exceptionClassName))
        {
            error = "用户名或密码错误";
        }
        else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName))
        {
            error = "用户名或密码错误";
        }
        else if ("jCaptcha.error".equals(exceptionClassName))
        {
            error = "验证码错误";
        }
        else if (exceptionClassName != null)
        {
            error = "系统繁忙";
        }
        else
        {
            // 如果已经登陆，直接跳到主页
            Subject subject = SecurityUtils.getSubject();
            if (subject.isAuthenticated()) {
            	map.put("user", super.getLoginUserObject());
                return "redirect:/home";
            }
            
        }
        
        if (req.getParameter("forceLogout") != null)
        {
            error = "您已经被管理员强制退出，请重新登录";
        }
        
        req.setAttribute("error", error);
        return "login";
    }
}
